package com.leyou.item.controller;

import com.leyou.dto.SpecGroupDto;
import com.leyou.dto.SpecParamDTO;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version V1.0
 * @author: Ljxpa
 * @date: 2020/03/06 23:08
 * @description:
 */
@RestController
@RequestMapping("/spec")
public class SpecGroupController {
	@Autowired
	private SpecGroupService specGroupService;

	@Autowired
	private SpecParamService specParamService;

	/**
	 * 添加参数组
	 *
	 * @param specGroupDto
	 * @return
	 */
	@PostMapping("/group")
	public ResponseEntity<Void> addSpecGroup(@RequestBody SpecGroupDto specGroupDto) {
		specGroupService.addGroup(specGroupDto);
		return ResponseEntity.ok().build();
	}

	/**
	 * 根据某个品类的id查询这个品类所有的参数组
	 *
	 * @param id 分类id
	 * @return 该分类下所有参数组的list集合
	 */
	@GetMapping("/groups/of/category")
	public ResponseEntity<List<SpecGroupDto>> queryGroupsByCid(@RequestParam Long id) {
		List<SpecGroupDto> list = specGroupService.queryGroupsByCid(id);
		return ResponseEntity.ok().body(list);
	}

	/**
	 * 根据组id查询组内所有规格参数
	 *
	 * @param gid 参数组id
	 * @param cid 品类的id
	 * @return 组内所有参数的list集合
	 */
	@GetMapping("/params")
	public ResponseEntity<List<SpecParamDTO>> queryParamsByGroupId(
			@RequestParam(required = false) Long gid,
			@RequestParam(required = false) Long cid
	) {
		return ResponseEntity.ok().body(specParamService.queryParamsByGroupId(gid, cid));
	}

	@PutMapping("/group")
	public ResponseEntity<Void> editSpecGroup(@RequestBody SpecGroupDto specGroupDto) {
		specGroupService.editSpecGroup(specGroupDto);
		return ResponseEntity.ok().build();
	}

	@DeleteMapping("/group/{id}")
	public ResponseEntity<Void> delSepcGroup(@PathVariable("id") Long gid) {
		specGroupService.delSpecGroupById(gid);
		return ResponseEntity.ok().build();
	}

	@DeleteMapping("/param/{id}")
	public ResponseEntity<Void> delSpecParam(@PathVariable("id") Long paramId) {
		specParamService.delSpecParamById(paramId);
		return ResponseEntity.ok().build();
	}

	@PutMapping("/param")
	public ResponseEntity<Void> editSpecParam(@RequestBody SpecParamDTO specParamDTO) {
		specParamService.editSpecParam(specParamDTO);
		return ResponseEntity.ok().build();
	}

	@PostMapping("/param")
	public ResponseEntity<Void> addSpecParam(@RequestBody SpecParamDTO specParamDTO) {
		specParamService.addSpecParam(specParamDTO);
		return ResponseEntity.ok().build();
	}

	/**
	 * 根据分类id查询出所有可供搜索的规格参数
	 * @param cid 分类id
	 * @param searching 是否可搜索
	 * @return 规格参数的集合
	 */
	@GetMapping("/params/searching")
	public ResponseEntity<List<SpecParamDTO>> queryParamsByGroupIdOrCategoryId(@RequestParam(required = false) Long cid, @RequestParam(required = false) Boolean searching) {
		List<SpecParamDTO> specParamDTOList = specParamService.querySearchingParamsByCid(cid, searching);
		return ResponseEntity.ok(specParamDTOList);
	}

}
